package ferp.center;

import java.util.Calendar;
import java.util.Date;

/**
 * User: igorgok
 * Date: 8/15/13 9:12 AM
 */
public class Message
{
  public static final class V1
  {
    public static final class Versions
    {
      public static final int NONE = 0;

      public static Versions range(int from, int to) {return new Versions(from, to);}
      public static Versions from(int code) {return new Versions(code, NONE);}
      public static Versions to(int code) {return new Versions(NONE, code);}

      public boolean fits(int code)
      {
        return (from == NONE || from <= code) && (to == NONE || code <= to);
      }

      private int from;
      private int to;

      // using NONE for "from" or "to" means ignoring this parameter
      private Versions(int from, int to)
      {
        this.from = from;
        this.to = to;
      }
    }

    public static final class Dates
    {
      public static final Date NONE = null;

      public static Dates at(int year, int month, int day)
      {
        return new Dates(new Date(Date.UTC(year, month, day, 0, 0, 0)), new Date(Date.UTC(year, month, day, 23, 59, 59)));
      }

      public static Dates range(int fy, int fm, int fd, int ty, int tm, int td)
      {
        Calendar from = Calendar.getInstance(), to = Calendar.getInstance();

        from.set(fy, fm - 1, fd, 0, 0, 0);
        to.set(ty, tm - 1, td, 23, 59, 59);

        return new Dates(from.getTime(), to.getTime());
      }

      public static Dates around(int year, int month, int day, int interval)
      {
        Calendar from = Calendar.getInstance(), to = Calendar.getInstance();

        from.set(year, month - 1, day, 0, 0, 0);
        from.add(Calendar.DATE, -interval);

        to.set(year, month - 1, day, 0, 0, 0);
        to.add(Calendar.DATE, +interval);
        to.add(Calendar.SECOND, -1);

        return new Dates(from.getTime(), to.getTime());
      }

      public boolean fits(Date date)
      {
        return (from == NONE || from.compareTo(date) <= 0) && (to == NONE || to.compareTo(date) >= 0);
      }

      // using NONE for "from" or "to" means ignoring this parameter
      private Dates(Date from, Date to)
      {
        this.from = from;
        this.to = to;
      }

      private Date from;
      private Date to;
    }

    public Versions version;
    public Dates date;
    public String text;

    public V1(Versions version, Dates date, String text)
    {
      this.version = version;
      this.date = date;
      this.text = text;
    }

    public V1(String text) {this(null, null, text);}

    public boolean fits(int version, Date date)
    {
      return (this.version == null || this.version.fits(version)) && (this.date == null || this.date.fits(date));
    }
  }

  // HTML space
  public static final String NBSP = "&nbsp;";

  // embedded image
  public static String image(String name) {return "{#" + name + "#}";}

  // version 1 of the object
  public final V1 v1;

  // constructor for version 1
  public Message(V1 v1) {this.v1 = v1;}
}
